package com.admin.controller.admin.upload;

import com.system.response.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Classname ImageUploadController
 * @Date 2023/10/10 9:21
 * @Created ZFC
 */
@RestController
@RequestMapping("/sys/upload")
public class ImageUploadController {

    @PostMapping("/image")
    public Result image(@RequestParam("file") MultipartFile[] files, HttpServletRequest request) throws IOException {
        System.out.println("files = " + files.length);
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            String filename = "11"+originalFilename.substring(originalFilename.lastIndexOf("."));
            System.out.println("filename = " + filename);
            String realPath = request.getSession().getServletContext().getRealPath("/websourses");
            File folder = new File(realPath);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            System.out.println("realPath = " + realPath);
            String destFilePath = String.format(realPath + "\\%s", filename);
            System.out.println("destFilePath = " + destFilePath);
            File destFile = new File(realPath, filename);
            System.out.println("destFile = " + destFile);
            //调用transferTo将上传的文件保存到指定的地址
            file.transferTo(destFile);

            System.out.println("filename = " + filename);
        }
        return new Result().setCode(200).setMessage("gs");
    }

}
